<template>
  <section>
    <el-form inline>
      <el-form-item label="选择公司">
        <el-select v-model="selCompany" placeholder="请选择" size="mini">
          <el-option v-for="company in companyList" :key="company.id" :label="company.companyName" :value="company.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="选择部门">
        <el-select v-model="selDepartment" placeholder="请选择" size="mini" value-key="id">
          <el-option v-for="department in departmentList" :key="department.id" :label="department.name" :value="department">
          </el-option>
        </el-select></el-form-item>
    </el-form>
  </section>
</template>

<script>
  import {mapGetters, mapActions, mapMutations} from 'vuex'
  import Vue from 'vue'//后续添加属性，需要vue联动
  import dateFormater from '@/common/date-formater'
  import errHandler from '@/assets/errHandler'
  export default {
    name: "selComDep",
    data() {
      return {
        companyList:null,
        selCompany:null,
        departmentList:null,
        selDepartment:null
      }
    },
    created(){
      console.log('selDep-----')
      //获取公司列表
      this.getCompanyList()
    },
    computed:{
    },
    watch: {
      selCompany(nv,ov){
        if(nv){
          //获取部门列表
          this.getDepartmentList(nv)
        }
      },
      selDepartment(nv,ov){
        if(nv){
          this.$emit('get',nv)
        }
      }
    },
    methods: {
      ...mapActions({
        fetch: 'fetch'
      }),
      getCompanyList(){
        this.fetch({name:"getCompanyList"})
          .then(res=>{
            console.log('companylist-----',res)
            this.companyList=res
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      getDepartmentList(companyId){
        this.fetch({name:"getDepartmentList",data:{companyId}})
          .then(res=>{
            console.log('deplist-----',res)
            this.departmentList=res
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      }
    }
  }
</script>

<style scoped>

</style>
